package code.class5;

/**
 * https://leetcode.cn/problems/find-greatest-common-divisor-of-array/
 * 找出数组的最大公约数
 */
public class Code02_findGCD {
    public static void main(String[] args) {
        int[] nums = {3,3};
        int gcd = findGCD(nums);
        System.out.println("gcd = " + gcd);
    }

    public static int findGCD(int[] nums) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        for (int n : nums) {
            min = Math.min(n, min);
            max = Math.max(n, max);
        }
        return gcd(max, min);
    }

    public static int gcd(int a, int b) {
        return b == 0 ? a : gcd(b, a % b);
    }
}
